package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.AdminLoginRequest;
import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.AdminMapper;
import com.example.springboot.service.IAdminService;
import com.example.springboot.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminService implements IAdminService {

    @Autowired
    AdminMapper adminMapper;

    private static final String PASS_SALT = "大道寺庙知世";
    private static final String DEFAULT_PASS = "1234";

    @Override
    public List<Admin> list() {
        return adminMapper.list();
    }

    @Override
    public PageInfo<Admin> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        List<Admin> admins = adminMapper.listByCondition(baseRequest);
        return new PageInfo<Admin>(admins);
    }

    @Override
    public void save(Admin admin) {
//        用户码部分
        Date date = new Date();
        // 用hutool生成uid
        Integer code = Math.abs(IdUtil.fastSimpleUUID().hashCode());
        String strcode = String.format("%08d", code);

        if (strcode.length() > 6) {
            strcode = strcode.substring(0, 6);
        }
        admin.setAdmincode(DateUtil.format(date,"yyyyMM") + strcode);

//        密码部分
        if (StrUtil.isBlank(admin.getPassword())){
            admin.setPassword(DEFAULT_PASS);
        }
        admin.setPassword(securePass(admin.getPassword()));
        adminMapper.save(admin);
    }

    @Override
    public Admin getById(Integer id) {
        return adminMapper.getByID(id);
    }

    @Override
    public void update(Admin admin) {
        adminMapper.update(admin);
    }

    @Override
    public void delById(Integer id) {
        adminMapper.delById(id);
    }

    @Override
    public LoginDTO login(AdminLoginRequest adminLoginRequest) {
        adminLoginRequest.setPassword(securePass(adminLoginRequest.getPassword()));
        Admin admin = adminMapper.loginByNameAndPassword(adminLoginRequest);
        if (admin == null){
            throw new ServiceException("用户名或密码错误");
        }

        LoginDTO dto = new LoginDTO();
        BeanUtils.copyProperties(admin,dto);

//        使用id和密码给与令牌token
        String token = TokenUtils.genToken(String.valueOf(admin.getId()), admin.getPassword());
        dto.setToken(token);
        return dto;
    }





    private String securePass(String password) {
        return  SecureUtil.md5(password + PASS_SALT);
    }
}
